<!--1.点击日志归档，跳转archives.php，-->
<!--2.显示这个日期文件下的所有文件-->
<!--3.有无$_GET['ym']，打开文件-->
<!--4.打开文件，获取下面文件的内容-->
<!--5.显示页面-->


<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/22
 * Time: 17:19
 */
header("Content-type: text/html; charset=utf-8");
session_start();
$login = false;
if ($_SESSION['user'] && $_SESSION['user'] === 'admin') {
    $login = true;
}

$folder_arr = array();
$post_data = array();
$class_arr = array();
//$_GET['ym'] = "201712";
$ok = false;
if (!isset($_GET['ym']) || empty($_GET['ym'])) {
    $ok = true;
    $msg = '请求参数错误；<a href="../index.php">返回首页 </a>';
}
$dir = "../diary";

$dh = @opendir($dir);
if ($dh) {
    while ($filename = readdir($dh)) {
        if ($filename !== '.' && $filename !== '..') {
            array_push($class_arr, $filename);

            if ($filename === $_GET['ym']) {
                $dirson = opendir($dir . '/' . $filename);
                if ($dirson) {
                    while ($fileson = readdir($dirson)) {
                        if ($fileson !== '.' && $fileson !== '..') {
                            array_push($folder_arr, $fileson);
                        }
                    }
                }
            }

        }
    }


//    获取文件夹下文件名称

}
rsort($folder_arr);
rsort($class_arr);

foreach ($folder_arr as $fileName) {
    $file_name = $dir . '/' . substr($fileName, 0, 6) . '/' . $fileName;

    if (file_exists($file_name)) {
        $fp = fopen($file_name, 'r');
        if ($fp) {
            flock($fp, LOCK_SH);
            $result = fread($fp, filesize($file_name));
        }
        flock($fp, LOCK_UN);
        fclose($fp);
    }
    $temp_data = array();
    $content_array = explode('|', $result);
    $temp_data['SUBJECT'] = $content_array[0];                //文章标题
    $temp_data['DATE'] = substr($content_array[1], 0, 4) . '-' . substr($content_array[1], 4, 2) . '-' . substr($content_array[1], 6, 2);                   //文章日期
    $temp_data['CONTENT'] = $content_array[2];               //文章内容
    $temp_data['FILENAME'] = $dir . '/' .substr($fileName,0,6). '/' . $fileName;            //文章目录
    array_push($post_data, $temp_data);
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>日志分类之后</title>
    <link rel="stylesheet" href="../css/blog.css">
</head>
<body>
<div class="box">
    <header>
        <h2>我的BLOG</h2>
    </header>
    <div class="content clearBox">
        <div class="left">
            <?php
            if($ok===false){
                foreach ($post_data as $post) {
            ?>
                <div>
                    <h5 class="title"><?php echo $post['SUBJECT']; ?></h5>
                    <dl class="blog-body">
                        <dt class="tit-date"><?php echo $post['DATE']; ?></dt>
                        <dd class="body-ctn"><?php echo $post['CONTENT']; ?></dd>
                    </dl>
                </div>
            <?php } }else{
                echo $msg;
            } ?>
        </div>

        <div class="right">
            <div class="sidebar">
                <h5 class="title">关于我</h5>
                <p class="right-ctn">php初学者</p>
            </div>
            <div class="sidebar">
                <h5 class="title">日志归档</h5>
                <?php
                foreach ($class_arr as $ym) {

                    $entry = $ym;
                    echo '<p class="right-ctn"><a href="archives.php?ym=' . $entry . '">' . substr($ym, 0, 4) . '-' . substr($ym, 4, 2) . '</a></p>';
                }  ?>

                <div class="side-foot">
                    <?php
                    if ($login) {
                        echo '<a href="logout.php">退出</a>';
                    } else {
                        echo ' <a href="../index.php">登录</a>';
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>

</div>
<div class="footer">CopyRight&nbsp;2017</div>

</body>
</html>